<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="reload">
            <div class="layui-form-item">

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <ul id="tree" class="dtree" data-id="0"></ul>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="city">
                            <option value="" >选择城市</option>
                            {volist name="city" id="vo"}
                            <option value="{$vo.name}">{$vo.name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="status">
                            <option value="">完成状态</option>
                            <option value="1">已完成</option>
                            <option value="2">未完成</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="time" id="time" placeholder="完成时间" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="keys" placeholder="工号|姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>
<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="view"><i class="layui-icon layui-icon-search"></i></button>
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'dtree', 'laydate'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let dtree = layui.dtree;
        let laydate = layui.laydate;
        let flex_id = "{$flex_id}";

        dtree.render({
            elem: "#tree",
            initLevel: "1",
            method: 'get',
            url: "/Members/getDepartment",
            select: true,
            selectTips: '选择部门',
        });

        laydate.render({
            elem: '#time'
            ,range: true //或 range: '~' 来自定义分割字符
            ,trigger: 'click'
        });

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '工号',
                    field: 'carolid',
                    align: 'center'
                },
                {
                    title: '姓名',
                    field: 'name',
                    align: 'center'
                },
                {
                    field:'division',
                    title: 'Division',
                    align:'center'
                },
                {
                    field:'department_name',
                    title: '部门',
                    align:'center'
                },
                {
                    title: '城市',
                    field: 'city',
                    align: 'center',
                },
                {
                    field:'status_name',
                    title: '完成状态',
                    align:'center',
                },
                {
                    field:'type',
                    title: '课程类型',
                    align:'center',
                },
                {
                    field:'time',
                    title: '提交时间',
                    align:'center',
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Flex/getDataList?flex_id='+flex_id,
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter',{
                title: '选课完成情况导出' //标题
                ,layEvent: 'export' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            },{
                title: '选课详情导出' //标题
                ,layEvent: 'export1' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            }]
        });

        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });
        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'view') {
                window.view(obj);
            }
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event === 'export') {
                window.export();
            } else if (obj.event === 'export1') {
                window.export1();
            }
        });

        window.view = function(obj) {
            layer.open({
                type: 2,
                title: '详情',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Flex/dataView?userid='+obj.data.userid+'&flex_id='+flex_id
            });
        }

        window.batchRemove = function(obj) {

            var checkIds = common.checkField(obj,'id');
            console.log(checkIds)
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除选中的数据吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/Flex/deleteData",
                    data:{id:checkIds, flex_id:flex_id},
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.status == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        // 数据导出
        window.export = function() {
            var field = form.val('reload');
            layer.confirm('确定要导出选课完成情况吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                window.open("{:url('exportData')}?city="+field.city+"&status="+field.status+"&time="+field.time+"&keys="+field.keys+"&flex_id="+flex_id+"&tree_select_nodeId="+field.tree_select_nodeId, "_blank");
            });
        };

        // 数据导出
        window.export1 = function() {
            var field = form.val('reload');
            layer.confirm('确定要导出选课详情吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                window.open("{:url('exportDataView')}?city="+field.city+"&status="+field.status+"&time="+field.time+"&keys="+field.keys+"&flex_id="+flex_id+"&tree_select_nodeId="+field.tree_select_nodeId, "_blank");
            });
        };
        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
